package com.wb3.meta.api.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wb3.meta.api.core.dao.MyToysMapper;
import com.wb3.meta.api.core.dao.UserChatListMapper;
import com.wb3.meta.api.core.model.UserChatList;
import com.wb3.meta.api.core.service.ToysChatMsgsService;
import com.wb3.meta.api.core.service.UserChatListService;
import com.wb3.meta.api.core.vo.*;
import com.wb3.meta.common.authentication.RequestHolder;
import com.wb3.meta.common.enums.EnumConstants;
import com.wb3.meta.common.mybatis.DataTableUtil;
import com.wb3.meta.common.pojo.ResultT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service("userChatListService")
@Transactional(transactionManager = "dataSourceTransactionManagerOne", rollbackFor = Exception.class)
public class UserChatListServiceImpl implements UserChatListService {
    @Autowired
    private UserChatListMapper userChatListMapper;

    @Autowired
    private MyToysMapper myToysMapper;

    @Autowired
    private ToysChatMsgsService toysChatMsgsService;
    /**
     * 分页查询方法
     *
     * @param vo
     * @return
     */
    @Override
    public Page<ResUserChatListVo> getModelPage(ReqUserChatListVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());

        return (Page<ResUserChatListVo>) getModelListByConditions(vo);
    }

    /**
     * 列表查询方法 -返回 res 对象
     *
     * @param vo
     * @return
     */
    @Override
    public List<ResUserChatListVo> getModelListByConditions(ReqUserChatListVo vo) {
        return userChatListMapper.getModelListByConditions(vo);
    }

    /**
     * 根据条件查询单条数据方法，返回res vo类
     *
     * @param vo
     * @return
     */
    @Override
    public ResUserChatListVo getResModelByConditions(ReqUserChatListVo vo) {
        return userChatListMapper.getResModelByConditions(vo);
    }

    /**
     * 根据条件查询单个对象方法
     *
     * @param userChatList
     * @return
     */
    @Override
    public UserChatList getModelByConditions(UserChatList userChatList) {
        return null;
    }

    /**
     * 根据id查询单个对象方法
     *
     * @param id
     * @return
     */
    @Override
    public UserChatList getModelById(Object id) {
        return userChatListMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增对象方法
     *
     * @param userChatList
     */
    @Override
    public void addModel(UserChatList userChatList) {
        userChatList.setCreateTime(new Date());
        userChatList.setStatus(EnumConstants.StatusEnum.VALID.getCode());
        userChatListMapper.insertUseGeneratedKeys(userChatList);
    }

    /**
     * 更新对象方法
     *
     * @param userChatList
     */
    @Override
    public void updateModel(UserChatList userChatList) {
        userChatList.setUpdateTime(new Date());
        userChatListMapper.updateByPrimaryKeySelective(userChatList);
    }

    /**
     * 根据id删除对象方法
     *
     * @param id
     */
    @Override
    public void deleteModelById(Object id) {

    }

    /**
     * 获取分页数据，返回 map对象方法
     *
     * @param vo
     * @return
     */
    @Override
    public ResultT getModelMapPage(ReqUserChatListVo vo) {
        return ResultT.success(DataTableUtil.changePage(getModelPage(vo)));
    }

    /**
     * 根据id查询单个对象方法，返回 map对象方法
     *
     * @param id
     * @return
     */
    @Override
    public ResultT getModelMapById(Object id) {
        return null;
    }

    /**
     * 根据条件查询单个对象，返回map对象方法
     *
     * @param userChatList
     * @return
     */
    @Override
    public ResultT getModelMapByConditions(UserChatList userChatList) {
        return null;
    }

    /**
     * 查询列表-map对象
     *
     * @param vo
     * @return
     */
    @Override
    public ResultT getResModelListMapByConditions(ReqUserChatListVo vo) {
        return ResultT.success(userChatListMapper.getModelListByConditions(vo));
    }

    /**
     * 根据条件查询res vo对象，返回map对象方法
     *
     * @param vo
     * @return
     */
    @Override
    public ResultT getResModelMapByConditions(ReqUserChatListVo vo) {
        return null;
    }

    /**
     * 更新或插入对象方法
     *
     * @param vo
     * @return
     */
    @Override
    public ResultT saveOrUpdate(ReqUserChatListVo vo) {
        if (vo.getId() != null) {
            updateModel(vo);
        } else {
            addModel(vo);
        }
        return ResultT.success(vo);
    }

    /**
     * 根据条件删除方法
     *
     * @param vo
     * @return
     */
    @Override
    public ResultT delete(ReqUserChatListVo vo) {
        return null;
    }


    @Override
    public ResultT createChat(ReqUserChatListVo reqUserChatListVo) {
        if (reqUserChatListVo.getMyToysId() == null) {
            return ResultT.fail("崽崽id不能为空");
        }

        ReqMyToysVo reqMyToysVo = new ReqMyToysVo();
        reqMyToysVo.setId(reqUserChatListVo.getMyToysId());
        reqMyToysVo.setUserId(RequestHolder.getUserId());
        ResMyToysVo resMyToysVo = myToysMapper.getResModelByConditions(reqMyToysVo);
        if (resMyToysVo == null) {
            return ResultT.fail("未查询到崽崽信息");
        }
        reqUserChatListVo.setChatBgUrl(resMyToysVo.getChatBgUrl());
        reqUserChatListVo.setUserId(RequestHolder.getUserId());
        ResUserChatListVo resUserChatListVo = getResModelByConditions(reqUserChatListVo);
        if (resUserChatListVo == null) {
            saveOrUpdate(reqUserChatListVo);
            //大姨，需要手动插入一条
            if (resMyToysVo.getToysId() == 11) {
                ReqToysChatMsgsVo reqToysChatMsgsVo = new ReqToysChatMsgsVo();
                reqToysChatMsgsVo.setMyToysId(reqMyToysVo.getId());
                reqToysChatMsgsVo.setUserChatListId(reqUserChatListVo.getId());
                reqToysChatMsgsVo.setToysTextContent("（斜眼看）最近工作怎么样呀？");
                toysChatMsgsService.saveOrUpdate(reqToysChatMsgsVo);
            }
        }else{
            return ResultT.success(resUserChatListVo.getId());
        }
        return ResultT.success(reqUserChatListVo.getId());
    }
}
